package com.click369.crm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.click369.crm.common.ResultVo;
import com.click369.crm.pojo.CStaff;
import com.click369.crm.service.StaffService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("staff")
public class StaffController implements BaseController<CStaff>{
	
	@Autowired
	private StaffService staffService;
	
	//添加
	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultVo save(CStaff s) {
		s.setId(null);
		staffService.save(s);
		return new ResultVo(200,"增加成功");
	}
	
	//根据id删除
	@RequestMapping("delete")
	@ResponseBody
	@Override
	public ResultVo delete(Integer id) {
		staffService.delete(id);
		return new ResultVo(200,"删除成功");
	}
	
	//修改
	@RequestMapping("update")
	@ResponseBody
	@Override
	public ResultVo update(CStaff s) {
		Integer id = s.getId();
		if(id == null){
			return new ResultVo(200,"没有id");
		}
		CStaff cstaff = staffService.getById(id);
		s.setCreateTime(cstaff.getCreateTime());
		staffService.update(s);
		return new ResultVo(200,"修改成功");
	}
	
	//查询所有分页
	@RequestMapping("list")
	@ResponseBody
	@Override
	public ResultVo select(@RequestParam(defaultValue="1")Integer page, 
			@RequestParam(defaultValue="3")Integer rows, CStaff s) {
		PageInfo info = staffService.findPage(page, rows, s);
		return new ResultVo(info.getTotal(), info.getList());
	}
	
	//根据id查询
	@RequestMapping("getById")
	@ResponseBody
	@Override
	public ResultVo getById(Integer id) {
		CStaff byid = staffService.getById(id);
		return new ResultVo(byid);
	}
	
	@RequestMapping("listAdmin")
	@ResponseBody
	public ResultVo selectAdmin(@RequestParam(defaultValue="1")Integer page, 
			@RequestParam(defaultValue="3")Integer rows) {
		PageInfo info = staffService.selectAdmin(page, rows);
		return new ResultVo(info.getTotal(), info.getList());
	}
}
